package com.orchard.orchardfarmmanagementsystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.orchard.orchardfarmmanagementsystem.dto.UserRoleDTO;
import com.orchard.orchardfarmmanagementsystem.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select("SELECT u.*, r.name AS roleName FROM user u LEFT JOIN user_roles ur ON u.user_id = ur.user_id LEFT JOIN roles r ON ur.role_id = r.role_id WHERE u.is_active = 1 AND r.name = 'worker'")
    List<Map<String, Object>> selectActiveUsersWithWorkerRole();

    @Select("SELECT " +
            "u.user_id AS userId, " +
            "u.username, " +
            "u.avatar_url AS avatarUrl, " +
            "u.email, " +
            "u.phone, " +
            "u.created_at AS createdAt, " +
            "u.is_active AS isActive,"+
            "r.description AS role " +
            "FROM user u " +
            "LEFT JOIN user_roles ur ON u.user_id = ur.user_id " +
            "LEFT JOIN roles r ON ur.role_id = r.role_id")
    List<UserRoleDTO> selectUsersWithRoles();
}